# python内置
import json
import xml.etree.ElementTree as ET
import ast
# 第三方库
# 自定义库
from configs.tools_configs import HANDLE_OPERATE_TYPES
from common.utils.message import fun_info


class CompressContent:
    def compress(cls,compress_type:str, content:str)->bool:
        if compress_type in HANDLE_OPERATE_TYPES:
            compress_flag,r_content = eval('cls.compress_'+compress_type)(content)
            if compress_flag:
                return fun_info(compress_flag,r_content)
            return fun_info(False,f'{compress_type} is illegal !')
        else:
            return fun_info(False,'not support unknown compress type !')
    
    def compress_JSON(cls,content):
        try:
            content = ast.literal_eval(content)
            gui_content = json.dumps(content,separators=(',', ':'))
        except ValueError:
            return False,None
        return True,gui_content


if __name__ == '__main__':
    HANDLE_OPERATE_TYPES = ['JSON','CSS','XML']
    CompressContent().compress('JSON',str({'a':1}))